#include "app_common.h"
#include "stdlib.h"
#include "sys/time.h"
#include "stdio.h"
#include "string.h"
long app_common_getCurrentTime(void)
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    return tv.tv_sec * 1000 + tv.tv_usec / 1000;

}

//把字节数组转为16进制字符串
char *app_common_bytes2hexstr(char *bytes, int len)
{
    char *hexstr = (char *)malloc(len * 2 + 1);
    int i;
    for (i = 0; i < len; i++)
    {
        sprintf(hexstr + i * 2, "%02X", bytes[i]);
    }
    hexstr[len * 2] = '\0';
    return hexstr;
}

//把16进制字符串转为字节数组 返回字节数组长度
int app_common_hexstr2bytes(char *hexstr, char* bytes_buff)
{
    int len = strlen(hexstr);
    int i;
    for (i = 0; i < len; i += 2)
    {
        char temp[3] = {hexstr[i],hexstr[i + 1],'\0'};
        sscanf(temp, "%2hhx", &bytes_buff[i / 2]);
    }
    return len / 2;
}

/**
 * @brief 把字符打印为16进制字符串
 */
void app_common_print_hex(  char *data, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        printf("%02x ", data[i]);
    }
    printf("\n");

}